<?php defined('SYSPATH') OR die('No direct access allowed.');

class Forum {

	public function __construct() {
		Event::add('forum.error', array(__CLASS__, 'show_error'));
		Event::add('forum.after.create.topic', array(__CLASS__, 'post_after_create'));
		Event::add('forum.before.delete.topic', array(__CLASS__, 'post_before_delete'));
		Event::add('forum.after.create.comment', array(__CLASS__, 'comment_after_create'));
		Event::add('forum.before.delete.comment', array(__CLASS__, 'comment_before_delete'));
		Event::add('forum.after.create.category', array(__CLASS__, 'category_after_create'));
		Event::add('forum.before.delete.category', array(__CLASS__, 'category_before_delete'));
		Event::add('auth.login', array(__CLASS__, 'user_login'));
	}

	static public function show_error() {
		if (is_null(Kohana::$instance)) {
			Kohana::$instance = new Forum_Controller;
		}
		$page = new View('forum/show_error');
		//$page->set('errortext', message::custom('error'));
		Kohana::$instance->template->content = $page;
		Kohana::$instance->template->render(TRUE);
    die();
	}

	static public function post_after_create() {
		// метод, вызываемый после создания нового топика
		$parent = ORM::factory('forum_category', Event::$data->category_id);
		if ($parent->loaded) {
			$parent->increase_count('post');
			$parent->last_post_id = Event::$data->id;
			$parent->save();
		}
		A2::instance()->get_user()->increase_count('post', 1);
	}

	static public function post_before_delete() {
		// метод, вызываемый до удаления топика
		$parent = ORM::factory('forum_category', Event::$data->category_id);
		if ($parent->loaded) {
			if ($parent->last_post_id == Event::$data->id) {
				$last_post = $parent->get_last_post();
				$parent->last_post_id = $last_post->loaded ? $last_post->id : NULL;
			}
			$parent->increase_count('post', -1);
			$parent->increase_count('comment', -1*Event::$data->comment_count);
			$parent->save();
		}
	}

	static public function comment_after_create() {
		$post = ORM::factory('forum_post', Event::$data->post_id);
		if ($post->loaded) {
			$post->last_comment_id = Event::$data->id;
			$post->category->increase_count('comment', 1);
			$post->comment_count++;
			$post->save();
		}
		A2::instance()->get_user()->increase_count('comment', 1);
	}

	static public function comment_before_delete() {
		$post = ORM::factory('forum_post', Event::$data->post_id);
		if ($post->loaded) {
			if ($post->last_comment_id == Event::$data->id) {
				$last_comment = $post->get_last_comment();
				$post->last_comment_id = $last_comment->loaded ? $last_comment->id : NULL;
			}
			$post->category->increase_count('comment', -1);
			$post->comment_count--;
			$post->save();
		}
	}

	static public function category_after_create() {
		if (Event::$data->parent_id > 0) {
			Event::$data->parent->increase_count('category');
		}
	}

	static public function user_login() {
		$userinfo = A2::instance()->get_user()->userinfo;
		if (!$userinfo->loaded) {
			// first login
			$userinfo->{$userinfo->primary_key} = A2::instance()->get_user()->primary_key_value;
			$userinfo->save();
		}
	}
}

new Forum;